package com.readboottest.concurrent;


import com.readboottest.ReadbootTestApplication;
import com.readboottest.chain.ReadChain;
import com.readboottest.constant.NovelConstant;
import com.readboottest.entity.Book;
import com.readboottest.entity.request.novel.WebsiteRequest;
import com.readboottest.enums.WebsiteEnum;
import com.readboottest.util.SpringUtil;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.DependsOn;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zhanghuiyong
 * @创建时间 2019年06月26日
 * @描述: 两个HTTP请求测试
 **/
@DependsOn("springUtils")
public class HttpRequestDemo {
    public static void main(String[] args) {
        ApplicationContext app = SpringApplication.run(ReadbootTestApplication.class, args);
        SpringUtil.setApplicationContext(app);
        Map<String, Object> bookMap = getBooKInfo();
        System.out.println(bookMap);

    }

    private static Map<String, Object> getBooKInfo() {
        Map<String, Object> map = null;
        try {
            map = new HashMap<>(4);
            long beginTime = System.currentTimeMillis();
            WebsiteRequest websiteRequestQD = new WebsiteRequest(WebsiteEnum.WEBSITE_QIDIAN.getWebsiteName(), NovelConstant.NOVEL_NAME_WSCQ);
            WebsiteRequest websiteRequestXBQ6 = new WebsiteRequest(WebsiteEnum.WEBSITE_XBIQUGE6.getWebsiteName(), NovelConstant.NOVEL_NAME_WSCQ);
            ReadChain readChain = new ReadChain();
            Book bookQD = (Book) readChain.execute(websiteRequestQD.getWebSiteName());
            long qdEndTime = System.currentTimeMillis();
            System.out.println("起点请求时间:" + (qdEndTime - beginTime));
            Book bookXBQ6 = (Book) readChain.execute(websiteRequestXBQ6.getWebSiteName());
            long bqEndTime = System.currentTimeMillis();
            System.out.println("笔趣请求时间:" + (bqEndTime - qdEndTime));

            System.out.println(bookQD);
            System.out.println(bookXBQ6);
            long endTime = System.currentTimeMillis();
            System.out.println("总请求时间:" + (endTime - beginTime));

            map.put("总时间", (endTime - beginTime));
            map.put("起点请求时间", (qdEndTime - beginTime));
            map.put("笔趣请求时间", (bqEndTime - qdEndTime));

        } catch (Exception e) {
            e.printStackTrace();
        }

        return map;
    }
}
